class Solution {
    public int strStr(String haystack, String needle) {
        if(needle.length() == 0) {
            return 0;
        }
        if(needle.length()>haystack.length()){
            return -1;
        }
        int n1 = 0,n2 = 0;
        int cur = n1;
        while(n1<haystack.length()&&n2<needle.length()) {
            if(haystack.charAt(n1) == needle.charAt(n2)) {
                n1++;
                n2++;
            }else {
                n1 = ++cur;
                n2 = 0;
            }
        }
        if(n1 == haystack.length()&&n2!=needle.length()) {
            return -1;
        }
        return cur;
    }
}